<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="背景颜色" prop="bgColor">
        <el-input v-model="formData.bgColor" placeholder="请输入背景颜色" />
      </el-form-item>
      <el-form-item label="背景图片" prop="bgUrl">
        <UploadImg v-model="formData.bgUrl" />
      </el-form-item>
      <el-form-item label="字体颜色" prop="color">
        <el-input v-model="formData.color" placeholder="请输入字体颜色" />
      </el-form-item>
      <el-form-item label="字体大小" prop="fontSize">
        <el-input v-model="formData.fontSize" placeholder="请输入字体大小" />
      </el-form-item>
      <el-form-item label="字体宽度" prop="fontWeight">
        <el-input v-model="formData.fontWeight" placeholder="请输入字体宽度" />
      </el-form-item>
      <el-form-item label="标签" prop="tag">
        <el-select
          v-model="formData.tag"
          filterable
          allow-create
          default-first-option
          :reserve-keyword="false"
          placeholder="请选择或输入标签"
        >
          <el-option v-for="item in tagOptions" :key="item" :label="item" :value="item" />
        </el-select>
      </el-form-item>
      <el-form-item label="内容" prop="message">
        <el-input v-model="formData.message" type="textarea" placeholder="请输入内容" />
      </el-form-item>
      <el-form-item label="@谁" prop="referTo">
        <el-input v-model="formData.referTo" type="textarea" placeholder="请输入@谁" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="formData.status">
          <el-radio
            v-for="dict in getIntDictOptions(DICT_TYPE.COMMON_STATUS)"
            :key="dict.value"
            :value="dict.value"
          >
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="审核状态" prop="approveStatus">
        <el-radio-group v-model="formData.approveStatus">
          <el-radio
            v-for="dict in getIntDictOptions(DICT_TYPE.BPM_PROCESS_INSTANCE_STATUS)"
            :key="dict.value"
            :value="dict.value"
          >
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input v-model="formData.sort" placeholder="请输入排序" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
import { BlogMessageApi, BlogMessageVO } from '@/api/cms/blogmessage'
import { AppSiteApi } from '@/api/cms/appsite'

/** 留言 表单 */
defineOptions({ name: 'BlogMessageForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  appSiteId: 0,
  appId: '',
  userType: undefined,
  userId: undefined,
  userNickname: undefined,
  userAvatar: undefined,
  bgColor: undefined,
  bgUrl: undefined,
  color: undefined,
  fontSize: undefined,
  fontWeight: undefined,
  tag: undefined,
  message: undefined,
  referTo: undefined,
  status: 0,
  approveStatus: 0,
  sort: 0,
  viewTimes: 0,
  thumbsUpTimes: 0,
  creatorIp: undefined,
  creatorCity: undefined
})
const formRules = reactive({})
const formRef = ref() // 表单 Ref

const tagOptions = ref([]) // 标签选项
/** 打开弹窗 */
const open = async (type: string, appSiteId: number, appId: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await BlogMessageApi.getBlogMessage(id)
    } finally {
      formLoading.value = false
    }
  } else {
    formData.value.appSiteId = appSiteId
    formData.value.appId = appId
  }
  await loadTags()
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

const loadTags = async () => {
  tagOptions.value = await AppSiteApi.getTagDictionary(formData.value.appId)
}
/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as BlogMessageVO
    if (formType.value === 'create') {
      await BlogMessageApi.createBlogMessage(data)
      message.success(t('common.createSuccess'))
    } else {
      await BlogMessageApi.updateBlogMessage(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    appSiteId: 0,
    appId: '',
    userType: undefined,
    userId: undefined,
    userNickname: undefined,
    userAvatar: undefined,
    bgColor: undefined,
    bgUrl: undefined,
    color: undefined,
    fontSize: undefined,
    fontWeight: undefined,
    tag: undefined,
    message: undefined,
    referTo: undefined,
    status: 0,
    approveStatus: 2,
    sort: 0,
    viewTimes: 100,
    thumbsUpTimes: 0,
    creatorIp: undefined,
    creatorCity: undefined
  }
  formRef.value?.resetFields()
}
</script>
